import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy_book.items import ScrapyBookItem



class BookSpider(scrapy.Spider):
    name = 'book'
    allowed_domains = ['books.toscrape.com']
    start_urls = ['http://books.toscrape.com/catalogue/page-1.html']


    def parse(self, response):
        xpath = '//li[@class="next"]'
        le = LinkExtractor(restrict_xpaths=xpath)
        links = le.extract_links(response)
        if links:
            next_link = links[0].url

            yield scrapy.Request(url=next_link,callback=self.parse)
        le = LinkExtractor(restrict_xpaths='//div[@class = "image_container"]')
        links = le.extract_links(response)
        for link in links:
            yield scrapy.Request(link.url,callback=self.parse_detail_page)

    def parse_detail_page(self,response):
        book=ScrapyBookItem()
        book['name']=response.xpath('//div[@class="col-sm-6 product_main"]/h1/text()').get()
        book['price'] = response.xpath('//div[@class="col-sm-6 product_main"]/p/text()').get()
        book['stars'] = response.xpath('//p[contains(@class,"star-rating")]/@class').get().split()[1]
        book['reviews'] = response.xpath('//table[@class="table table-striped"]/tr[last()]/td/text()').get()
        yield book
